Shrink Virtual Hard Disk Size

Free disk space. Decrease the size of a virtual disk. Decrease virtual machine storage capacity.
Introduction
[edit]Over the course of many system upgrades, logging, and similar activity, the image files can grow substantially. Deletion of obsolete files does not automatically shrink the disk size back, because it has grown to accommodate the data.
Overview
[edit]Note: When you create a large file or download a large file and then delete it from the virtual machine (VM), the space will not be freed automatically. Disk shrinking is required.
In summary, the following steps are required to shrink the disk.
1 Delete files to free space.
2 Remove the bdev_allow_write_mounted=0 kernel parameter.
Elaborated below.
3 Reboot your virtual machine in live mode - sysmaint session.
4 Inside the VM: Apply the zerofree procedure.
5 Shut down the VM.
6 On the host operating system (OS): Run a virtualizer specific command to release the freed space.
7 Done.
Steps
[edit]Platform specific. Select your virtualizer.
- Non-Qubes: See below.
- Qubes: Undocumented. Unspecific to Kicksecure. Self Support First Policy applies. Refer to Qubes documentation.
A tool called zerofree is needed to shrink the backing virtual disk file. Since the defaults are ext4 partitions, the process might be a bit involved:
1 Inside the VM:
2 Install the package zerofree in the virtual machine you want to shrink.
Install package(s) zerofree following these instructions:
1 Platform specific notice.
- Kicksecure: No special notice.
- Kicksecure-Qubes: In Template.
2 Update the package lists and upgrade the system.
sudo apt update && sudo apt full-upgrade
3 Install the zerofree package(s).
Using apt command line --no-install-recommends option is in most cases optional.
sudo apt install --no-install-recommends zerofree
4 Platform specific notice.
- Kicksecure: No special notice.
- Kicksecure-Qubes: Shut down Template and restart App Qubes based on it as per Qubes Template Modification.
5 Done.
The procedure of installing package(s) zerofree is complete.
3 Safe space inside the VM.
Delete what you no longer need in the VM, and run apt-get purge, apt-get autoremove, apt-get clean, etc. as needed.
4 Reboot your virtual machine in live mode - sysmaint session. [1]
5 Remove the bdev_allow_write_mounted=0 kernel parameter.
Either by making a Temporary Kernel Boot Parameter Change or Permanent Configuration Changes. [2]
6 Run zerofree.
Platform specific. Select your virtualizer.
Qubes
Undocumented. Unspecific to Kicksecure. Self Support First Policy applies.
Refer to the Qubes OS user documentation.
7 Shut down the VM.
8 On the host:
9 Compact the host VM image.
Platform specific. Select your virtualizer.
VirtualBox
Host operating system specific.
- Linux: VBoxManage modifymedium disk “/path/to/disk.vdi” --compact
- Windows: VBoxManage.exe modifymedium disk “C:\path\to\disk.vdi” --compact
Unspecific to Kicksecure. Self Support First Policy applies.
10 Done.
forum discussions:
KVM
10 Open a terminal on the host.
11 Switch to root.
sudo -s
12 Change directory to the /var/lib/libvirt/images folder.
cd /var/lib/libvirt/images
13 View files inside the /var/lib/libvirt/images folder.
ls
14 Create a backup of the disk you want to shrink by moving YourVirtualMachineDisk.qcow2 to YourVirtualMachineDisk.qcow2.backup.
Note:
- Replace
YourVirtualMachineDisk.qcow2with the actual name of your virtual hard disk. - Replace
YourVirtualMachineDisk.qcow2.backupwith the actual name of your virtual hard disk.
mv YourVirtualMachineDisk.qcow2 YourVirtualMachineDisk.qcow2.backup
15 Shrink the disk using qemu-img.
qemu-img convert -O qcow2 -p YourVirtualMachineDisk.qcow2.backup YourVirtualMachineDisk.qcow2
16 Boot up the VM and see if it is working. If it is, you can delete the backup of the qcow2 file.
17 Done.
There are more advanced approaches using thin provisioned images based on a static qcow2 backing file, which is often preferred. However, the above is a basic method for producing a smaller qcow2 disk file. [3]
forum discussion: Is it possible to re-shrink the qcow2 image files?![]()
Qubes
Undocumented. Unspecific to Kicksecure. Self Support First Policy applies.
Refer to the Qubes OS user documentation.
Build from Source Code
[edit]Refer to Build and Update Kicksecure from Source Code and utilize this setting:
--vmsize 50G
Future
[edit]Either:
- A
dracutmodule, or - B
mkosi-initrdsystemd unit,
that runs zerofree before mounting the root disk would be useful. Such a feature does not exist yet in any Linux distribution.
Forum Discussion
[edit]- The Workstation VMDK gets bigger and bigger

- https://forums.whonix.org/t/automate-recovering-free-space-from-vdi-disks/22467

See Also
[edit]Footnotes
[edit]- ↑ Alternatively, booting into persistent mode - recovery mode would also be possible. But in that case, additional commands would need to be typed. systemctl stop systemd-journald.socket systemctl stop systemd-journald.service mount -o remount,ro /
- ↑
https://forums.whonix.org/t/how-to-compress-and-prevent-vdi-from-ballooning-after-each-update-deleting-large-files/22675/11

- ↑
Credit goes to forum user tempest

: post in "Is it possible to re-shrink the qcow2 image files?"

We believe security software like Kicksecure needs to remain Open Source and independent. Would you help sustain and grow the project? Learn more about our 13 year success story and maybe DONATE!